package cn.javabb.web.interceptor;

import cn.javabb.common.utils.RequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义拦截器
 * @Author QINB imqinbao@163.com
 * @CreateDate 2019/1/11/011 17:53
 * @Since V1.0
 */
@Slf4j
@Component
public class MyInterceptor implements HandlerInterceptor {

    private static final String USER_AGENT = "user-agent";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String contextPath = request.getContextPath();
        // System.out.println(contextPath);
        String uri = request.getRequestURI();
        //log.info("UserAgent: {}", request.getHeader(USER_AGENT));
        log.info("用户访问地址: {}, 来路地址: {}", uri, RequestUtil.getIp());
        /*UserDO userDO = ShiroUtil.getLoginUser();

        // 请求过滤，如果要访问 /admin 开头的接口都需要登陆用户
        if (uri.startsWith(contextPath + "/admin") && !uri.startsWith(contextPath + "/admin/login") && null == userDO) {
            response.sendRedirect(request.getContextPath() + "/admin/login");
            return false;
        }*/
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        //request.setAttribute("thUtil",thUtil);//封装thymeleaf的一些工具类

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
